package com.qiwx.pigeon.activity.home;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.adapter.home.CageRepairAdapter;
import com.qiwx.pigeon.data.params.CageParam;
import com.qiwx.pigeon.data.vo.CageRepairBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;


import java.util.ArrayList;
import java.util.List;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-06 10:31
 * 鸽笼维修
 **/
public class CageRepairActivity extends BaseRemindActivity {

    List<CageRepairBean> data = new ArrayList<>();
    int current_pos=-1;

//    @Override
//    public int getLayoutId() {
//        return R.layout.activity_cage_repair;
//    }
   CageParam cageParam;
    @Override
    public void initAfterOnCreate() {
        cageParam=new CageParam();
        cageParam.setPageSize(pageSize);
        initView();
        // xRefreshView.startRefresh();
        //loadData(true);
    }

    //查询总数
//    public void queryAllAmount() {
//        allCount = LitePal.where("wh_id=?", wh_id + "").count(CageRepairModel.class);
//        txtAmount.setText(allCount + "");
//
//    }
    @Override
    public void loadData(boolean isRefresh) {
        if (isRefresh) {
            pageNo = 1;
            data.clear();
            //queryAllAmount();
        } else {
            pageNo++;
        }

        cageParam.setPageNum(pageNo);
        cageParam.setWh_id(wh_id);
        cageParam.setIs_adjust(0);
        mPresenter.queryCageRepairByPage(cageParam);
//        int offset = (pageNo - 1) * pageSize;
////        String sql="select * from pigeonpairmodel where wh_id="+wh_id;
//        List<CageRepairModel> list = LitePal
//                .limit(pageSize)
//                .order("c_number")
//                .offset(offset)
//                .where("wh_id=?", wh_id + "").find(CageRepairModel.class);
//        if (list != null && list.size() > 0) {
//            for (int i = 0; i < list.size(); i++) {
//                CageRepairModel model = list.get(i);
//                CageRemindBean bean = new CageRemindBean();
//                bean.setC_id(model.getId());
//                bean.setC_number(model.getC_number());
//                bean.setRepairContent(getRePairContent(model));
//                data.add(bean);
//            }
//        }
//        if (isRefresh) {
//            xRefreshView.stopRefresh();
//        } else {
//            xRefreshView.stopLoadMore();
//        }
//        if (data.size() == 0) {
//            xRefreshView.setVisibility(View.GONE);
//            txtNodata.setVisibility(View.VISIBLE);
//        } else {
//            xRefreshView.setVisibility(View.VISIBLE);
//            txtNodata.setVisibility(View.GONE);
//            if (data.size() < allCount) {
//                xRefreshView.setPullLoadEnable(true);
//            } else {
//                if (!isRefresh) {
//                    MyToast.showLong("已经没有更多了");
//                }
//                xRefreshView.setPullLoadEnable(false);
//
//            }
//        }
//        adapter.notifyDataSetChanged();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        adapter = new CageRepairAdapter(R.layout.adapter_remind_common_list_item, data);
        return adapter;
    }

    @Override
    public int getSize() {
        return data.size();
    }

    @Override
    public int addListViewHead() {
        return R.layout.cage_remind_headview;
    }

    @Override
    public void initView() {
        super.initView();
        titleReturn.changeArrowLeft();
        titleContent.setText("待维修鸽笼详情列表");
        txtAmountName.setText("待维修合计(个)：");


        if (adapter != null && adapter instanceof CageRepairAdapter) {
            ((CageRepairAdapter) adapter).setIPigeonPairInterface((pos, c_id, methodId) -> {
                cageModify(pos, c_id);
            });
        }
    }

    //鸽笼维修
    public void cageModify(int pos, int c_id) {
        current_pos=pos;
       CageParam param=new CageParam();
       param.setC_id(c_id);
       param.setIs_history(1);
        mPresenter.updateCageRepair(param);
//        CageRepairModel model = LitePal.find(CageRepairModel.class, c_id);
//        if (model != null) {
//            int rows = model.delete();
//            if (rows > 0) {
//                MyToast.showLong("操作成功");
//                allCount = allCount - 1;
//                txtAmount.setText(allCount + "");
//                data.remove(pos);
//                adapter.notifyDataSetChanged();
//            }
//        } else {
//            MyToast.showLong("操作失败");
//
//        }
    }


    @Override
    public void onQueryCageRepairSuccess(PageInfo<CageRepairBean> bean) {
        if (bean != null && bean.getList() != null) {
            data.addAll(bean.getList());
        }
        super.onQueryCageRepairSuccess(bean);
    }

    @Override
    public void onUpdateCageStatusSuccess(int type) {
        if (current_pos >= 0) {
            allCount = allCount - 1;
            txtAmount.setText(allCount + "");
            data.remove(current_pos);
            adapter.notifyDataSetChanged();
            current_pos = -1;
        }

    }
}
